from Gene import *

class ADN:
    """Class ADN
    """
    # Attributes:
    __listeGene = {}  # (List <Gene>)
    
    # Operations
    def __init__(self):
        """function ADN
        
        returns 
        """
        self.__listeAlleles = {}
        self.initListeAllele();
    
    def ajouterGene(self, in_gene):
        """function AjouterGene
        
        in_gene: <Gene>
        
        returns boolean
        """
        if(isinstance(in_gene, Gene)):
            in_gene.setId(len(self.__listeGene))
            self.__listeGene[in_gene.getId()] = in_gene
            return True
        else:
            return False

    def reindex(self):
        compteur = 0
        for gene in self.__listeGene.values():
            # @type gene Gene
            for allele in gene.getAlleles().values():
                # @type allele Allele
                allele.setId(compteur)
                compteur += 1
    
    def identifier(self, in_ADN):
        """function identifier
        
        in_ADN: String
        
        returns 
        """
        return None # should raise NotImplementedError()
    
    def getGenes(self):
        """function getGenes
        
        returns List
        """
        return self.__listeGene # should raise NotImplementedError()

    def getGenesName(self):
        names = []
        for g in self.__listeGene.values():
            # @type g Gene
            names.append(g.getName())
        return names

    
    def identify(self, in_gene):
        """function getAllele
        
        in_gene: String
        
        returns <Allele>
        """
        if(self.__listeGene.has_key(in_gene)):
            return self.__listeGene[in_gene].getDefaultAllele()
        return None

    def listeAlleles(self):
        listeAlleles = []
        values = self.__listeGene.values()
        for gene in values:
            for allele in gene.getAlleles().values():
                listeAlleles.append(allele)
        return listeAlleles

    def getGeneByName(self, in_name):
        for gene in self.__listeGene.values():
            if gene.getName() == in_name:
                return gene
        return None

    def getGeneById(self, in_key):
        if self.__listeGene.has_key(in_key):
            return self.__listeGene[in_key]
        return None

    def initListeAllele(self):
        values = self.listeAlleles()
        for allele in values:
            # @type allele Allele
            self.__listeAlleles[allele.getId()] = allele

    def getListeAlleles(self):
        return self.__listeAlleles

    def getIdOfAllele(self, in_allele):
        print in_allele
        print "-----------"
        compteur = 0
        for allele in self.__listeAlleles.values():
            print allele
            if allele == in_allele:
                return compteur
            compteur += 1
        return None

    
    def __str__(self):
        retour = ""
        for gene in self.__listeGene.values():
            retour += str(gene) + "/"
        return retour
